//
//  HpProductModel.swift
//
//  Created by eme on 2016/12/14
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol

public class HpProductModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kHpProductGprrptsortKey: String = "gprrptsort"
    internal let kHpProductRptsortKey: String = "rptsort"
    internal let kHpProductPdnameKey: String = "pdname"
    internal let kHpProductIstimebyKey: String = "istimeby"
    internal let kHpProductPdidKey: String = "pdid"
    internal let kHpProductRptcodeKey: String = "rptcode"
    internal let kHpProductImg1Key: String = "img1"
    internal let kHpProductIsactiveKey: String = "isactive"
    internal let kHpProductMoqKey: String = "moq"
    internal let kHpProductSkuidKey: String = "skuid"
    internal let kHpProductPddesKey: String = "pddes"
    internal let kHpProductRptimgKey: String = "rptimg"
    internal let kHpProductBridKey: String = "brid"
    internal let kHpProductProfitKey: String = "profit"
    internal let kHpProductPtnumKey: String = "ptnum"
    internal let kHpProductRptnameKey: String = "rptname"
    internal let kHpProductShowtypeKey: String = "showtype"
    internal let kHpProductRptidKey: String = "rptid"
    internal let kHpProductEndtimeKey: String = "endtime"
    internal let kHpProductBegintimeKey: String = "begintime"
    internal let kHpProductShpriceKey: String = "shprice"
    internal let kHpProductSalecountKey: String = "salecount"
    
    
    // MARK: 属性
    public var gprrptsort: Int
    public var rptsort: Int
    public var pdname: String
    public var istimeby: String
    public var pdid: Int
    public var rptcode: String
    public var img1: String
    public var isactive: String
    public var moq: Int
    public var skuid: Int
    public var pddes: String
    public var rptimg: String
    public var brid: String
    public var profit: Double
    public var ptnum: Int
    public var rptname: String
    public var showtype: String
    public var rptid: Int
    public var endtime: String
    public var begintime: String
    public var shprice: Double
    public var salecount: Int
    public var brname:String
    public var stock:Int
    public var pdunit:String
    public var minshprice:Double
    public var bdprice:Double
    //是否商品分类（0-否1-是）
    public var ptisproduct:String
    /// 不等于3失效商品
    public var pdstate:String
    
    
    
    ///抢购相关字段rush
    public var rush_stock_desc:String{
        get{
            if self.stock == 0 {
                return "补货中…"
            }else{
                return "仅剩\(self.stock)\(self.pdunit)"
            }
        }
    }
    public var rush_stock_percent:Double{
        get{
            let total = Double(self.stock + self.salecount)
            if total > 0 {
                return Double(self.salecount)/total
            }else{
                return 1.0
            }
        }
    }
    
    
    
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    public required  init?(json: JSON) {
        gprrptsort = json[kHpProductGprrptsortKey].intValue
        rptsort = json[kHpProductRptsortKey].intValue
        pdname = json[kHpProductPdnameKey].stringValue
        istimeby = json[kHpProductIstimebyKey].stringValue
        pdid = json[kHpProductPdidKey].intValue
        rptcode = json[kHpProductRptcodeKey].stringValue
        img1 = json[kHpProductImg1Key].stringValue
        isactive = json[kHpProductIsactiveKey].stringValue
        moq = json[kHpProductMoqKey].intValue
        skuid = json[kHpProductSkuidKey].intValue
        pddes = json[kHpProductPddesKey].stringValue
        rptimg = json[kHpProductRptimgKey].stringValue
        brid = json[kHpProductBridKey].stringValue
        profit = json[kHpProductProfitKey].doubleValue
        ptnum = json[kHpProductPtnumKey].intValue
        rptname = json[kHpProductRptnameKey].stringValue
        showtype = json[kHpProductShowtypeKey].stringValue
        rptid = json[kHpProductRptidKey].intValue
        endtime = json[kHpProductEndtimeKey].stringValue
        begintime = json[kHpProductBegintimeKey].stringValue
        shprice = json[kHpProductShpriceKey].doubleValue
        salecount = json[kHpProductSalecountKey].intValue
        brname = json["brname"].stringValue
        stock = json["stock"].intValue
        pdunit = json["pdunit"].stringValue
        minshprice = json["minshprice"].doubleValue
        bdprice = json["bdprice"].doubleValue
        ptisproduct = json["ptisproduct"].stringValue
        pdstate = json["pdstate"].stringValue
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    public func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(gprrptsort as AnyObject, forKey: kHpProductGprrptsortKey)
        
        dictionary.updateValue(rptsort as AnyObject, forKey: kHpProductRptsortKey)
        
        dictionary.updateValue(pdname as AnyObject, forKey: kHpProductPdnameKey)
        
        dictionary.updateValue(istimeby as AnyObject, forKey: kHpProductIstimebyKey)
        
        dictionary.updateValue(pdid as AnyObject, forKey: kHpProductPdidKey)
        
        dictionary.updateValue(rptcode as AnyObject, forKey: kHpProductRptcodeKey)
        
        dictionary.updateValue(img1 as AnyObject, forKey: kHpProductImg1Key)
        
        dictionary.updateValue(isactive as AnyObject, forKey: kHpProductIsactiveKey)
        
        dictionary.updateValue(moq as AnyObject, forKey: kHpProductMoqKey)
        
        dictionary.updateValue(skuid as AnyObject, forKey: kHpProductSkuidKey)
        
        dictionary.updateValue(pddes as AnyObject, forKey: kHpProductPddesKey)
        
        dictionary.updateValue(rptimg as AnyObject, forKey: kHpProductRptimgKey)
        
        dictionary.updateValue(brid as AnyObject, forKey: kHpProductBridKey)
        
        dictionary.updateValue(profit as AnyObject, forKey: kHpProductProfitKey)
        
        dictionary.updateValue(ptnum as AnyObject, forKey: kHpProductPtnumKey)
        
        dictionary.updateValue(rptname as AnyObject, forKey: kHpProductRptnameKey)
        
        dictionary.updateValue(showtype as AnyObject, forKey: kHpProductShowtypeKey)
        
        dictionary.updateValue(rptid as AnyObject, forKey: kHpProductRptidKey)
        
        dictionary.updateValue(endtime as AnyObject, forKey: kHpProductEndtimeKey)
        
        dictionary.updateValue(begintime as AnyObject, forKey: kHpProductBegintimeKey)
        
        dictionary.updateValue(shprice as AnyObject, forKey: kHpProductShpriceKey)
        
        dictionary.updateValue(salecount as AnyObject, forKey: kHpProductSalecountKey)
        
        
        return dictionary
    }
    
    
}
